package action.back;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import utils.ReflectionUtils;

public class LvAction extends BaseAction{
	
	@SuppressWarnings("unchecked")
	public void list() throws Exception{
		
		String c = request.getParameter("c");
		String l = request.getParameter("l");
		String v = request.getParameter("v");
		List<Object> list = getCommonService().findAll("from " + c);
		
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		for(Object o : list){
			Object l_v = ReflectionUtils.getFieldValue(o, l);
			Object v_v = ReflectionUtils.getFieldValue(o, v);
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("label", l_v);
			m.put("value", v_v);
			resultList.add(m);
		}
		
		JSONArray jsonObject = JSONArray.fromObject(resultList);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
	}
}
